<template>
	<u-index-list :scrollTop="scrollTop">
		<view v-for="(item, index) in address_list" :key="index">
			<u-index-anchor :use-slot="true">
				<text class="anchor-text u-padding-left-10">{{item.case}}</text>
			</u-index-anchor>
			<view class="list-cell" @click="chose_address(ite)" v-for="(ite , ind) in item.list" :key='ind'>
				{{ite.region_name}}
			</view>
		</view>
	</u-index-list>
</template>

<script>
	export default {
		data() {
			return {
				from: 1, // 来向
				address_list: [],
				scrollTop: 0,
			}
		},
		methods: {
			chose_address(item) {
				if (this.from == 1) {
					this.$u.vuex('vuex_address_dafauflt', item);
				} else {
					this.$u.vuex('vuex_address_search', item);
				}
				// console.log(this.vuex_address_search) ;
				this.$u.route({
					type: 'back',
				})
			}
		},
		onLoad(optons) {
			// from 1 : 来自首页 ; 2 : 来自搜索页面
			this.from = optons.from;
			this.$u.api.regionList({}).then(res => {
				let temp_address_list = [];
				res.list.forEach( (items , index) => {
					let flag = !temp_address_list.some((item, i, val) => {
						return items.case == item.case
					});
					if (flag) {
						temp_address_list.push({
							case: items.case,
							list: [],
						})
					}
				} )
				for(let i = 0 ; i < temp_address_list.length ; i++ ){
					for(let j = 0 ; j < res.list.length ; j ++) {
						if(temp_address_list[i].case == res.list[j].case){
							temp_address_list[i].list.push(res.list[j])
						}
					}
				}
				this.address_list = temp_address_list;
			})
		},
		onPageScroll(e) {
			this.scrollTop = e.scrollTop;
		}
	}
</script>

<style lang="scss" scoped>
	.list-cell {
		display: flex;
		box-sizing: border-box;
		width: 100%;
		padding: 10px 24rpx;
		overflow: hidden;
		color: #323233;
		font-size: 14px;
		line-height: 24px;
		background-color: #fff;
	}

	.anchor-text {
		color: red;
		
	}
</style>
